//坦克的颜色
var heroColor=new Array("#BA9658","#FEF26E");
var enemyColor=new Array("#00A2B5","#00FEFE");
function bullet(x,y,direct){
	this.x=x;
	this.y=y;
	this.direct=direct;
	this.speed=5;
	this.timer=null;
	this.isalive=true;
	this.run=function(){
//		console.log(maincan.width)
		if(this.x<=0||this.x>=maincan.width||this.y<=0||this.y>=maincan.height){
			window.clearInterval(this.timer);
			this.isalive=false;
			this.timer=null;
		}else{
			switch(this.direct){
				case 0:
				this.y-=this.speed;
				break;
				case 1:
				this.x+=this.speed;
				break;
				case 2:
				this.y+=this.speed;
				break;
				case 3:
				this.x-=this.speed;
				break;
			}
		}
		if(!herobullets[herobullets.length-1].timer)//限定最大发射子弹数，当前代表最后一个定时器停止工作
			herobullets.splice(0,herobullets.length);//清空数组
	}
}
function tank(x,y,direct,speed,color){
				this.x=x;
				this.y=y;
				this.direct=direct;
				this.speed=speed;
				this.color=color;
				this.isalive=true;
				this.moveUp=function(){
					this.y-=this.speed;
					this.direct=0;
				}
				this.moveRight=function(){
					this.x+=this.speed;
					this.direct=1;
				}
				this.moveDown=function(){
					this.y+=this.speed;
					this.direct=2;
				}
				this.moveLeft=function(){
					this.x-=this.speed;
					this.direct=3;
				}
		}
function hero(x,y,direct,speed,color){
	//通过对象冒充达到继承的效果
	this.tank=tank;
	this.tank(x,y,direct,speed,color);
	this.shot=function(){
		switch(this.direct){
			case 0:
			var herobullet=new bullet(this.x+18,this.y,this.direct);
			break;
			case 1:
			var herobullet=new bullet(this.x+60,this.y+18,this.direct);
			break;
			case 2:
			var herobullet=new bullet(this.x+18,this.y+60,this.direct);
			break;
			case 3:
			var herobullet=new bullet(this.x,this.y+18,this.direct);
			break;
		}
			herobullets.push(herobullet);
			
			herobullets[herobullets.length-1].timer=window.setInterval("herobullets["+(herobullets.length-1)+"].run()",50);
//		herobullet=new bullet(this.x,this.y,0);
//		console.log(herobullets)
	}
}
function enemy(x,y,direct,speed,color){
	//通过对象冒充达到继承的效果
	this.tank=tank;
	this.tank(x,y,direct,speed,color);
	this.movetimer=null;
}
//绘制子弹
function drawbullet(){
	for(var i=0;i<herobullets.length;i++){
		var herobullet=herobullets[i];
		if(herobullet!=null&&herobullet.isalive){
	//		alert("d")
			console.log(herobullet.x);
			cxt.fillStyle="#FEF26E";
			cxt.fillRect(herobullet.x,herobullet.y,4,4);
		}
	}
}
//绘制坦克
function drawtank(tank){
//				alert("k")
//				var tankimg=new Image();
//				tankimg.src="/Home/Public/images/"+tank.color+"tank"+code+".jpg";
//				tankimg.onload = function(){
//					cxt.drawImage(tankimg,tank.x,tank.y,100,100);
//				}
	 if(tank.isalive){
	 	//考虑方向  
            switch(tank.direct){  
  
            case 0: //上  
            case 2:// 下  
                //画出自己的坦克，使用前面的绘图技术  
                //设置颜色  
                cxt.fillStyle=tank.color[0];  
                //韩老师使用 先死--->后活 (初学者最好用这个方法)  
                //先画出左面的矩形  
                cxt.fillRect(tank.x,tank.y,10,60);  
                //画出右边的矩形(这时请大家思路->一定要一个参照点)  
                cxt.fillRect(tank.x+30,tank.y,10,60);  
                //画出中间矩形  
                cxt.fillRect(tank.x+12,tank.y+10,16,40);  
                //画出坦克的盖子  
                cxt.fillStyle=tank.color[1];  
                cxt.beginPath(); 
                cxt.arc(tank.x+20,tank.y+30,8,0,360,true);  
                cxt.fill();
                cxt.closePath();  
                //画出炮筒(直线)  
                cxt.strokeStyle=tank.color[1];  
                //设置线条的宽度  
                cxt.lineWidth=3;  
                cxt.beginPath();  
                cxt.moveTo(tank.x+20,tank.y+30);  
                  
                if(tank.direct==0){  
                	cxt.lineTo(tank.x+20,tank.y);  
                }else if(tank.direct==2){  
                	cxt.lineTo(tank.x+20,tank.y+60);  
                }  
  
                cxt.closePath();  
                cxt.stroke();  
                break;  
            case 1: //右和左  
            case 3:  
                //画出自己的坦克，使用前面的绘图技术  
                //设置颜色  
                cxt.fillStyle=tank.color[0];  
                //韩老师使用 先死--->后活 (初学者最好用这个方法)  
                //先画出左面的矩形  
                cxt.fillRect(tank.x,tank.y,60,10);  
                //画出右边的矩形(这时请大家思路->一定要一个参照点)  
                cxt.fillRect(tank.x,tank.y+30,60,10);  
                //画出中间矩形  
                cxt.fillRect(tank.x+10,tank.y+12,40,16);  
                //画出坦克的盖子  
                cxt.fillStyle=tank.color[1]; 
                cxt.beginPath();
                cxt.arc(tank.x+30,tank.y+20,8,0,Math.PI*2,true);
                cxt.closePath();  
                cxt.fill();  
                //画出炮筒(直线)  
                cxt.strokeStyle=tank.color[1];  
                //设置线条的宽度  
                cxt.lineWidth=3;  
                cxt.beginPath();  
                cxt.moveTo(tank.x+30,tank.y+20);  
                //向右  
                if(tank.direct==1){  
                cxt.lineTo(tank.x+60,tank.y+20);  
                }else if(tank.direct==3){ //向左  
                cxt.lineTo(tank.x,tank.y+20);  
                }  
  
                cxt.closePath();  
                cxt.stroke();  
                break;  
  
            } 
	 }
}
function ishit(bullet,tank){//判断是否击中坦克
	//判断子弹坐标是否在坦克所在的矩形内
	//矩形右下角坐标
	var rightdownx;
	var rightdowny;
	switch(tank.direct){  
  
            case 0: //上  
            case 2:// 下 
            rightdownx=tank.x+40;
            rightdowny=tank.y+60;
            break;
            case 1: //右  
            case 3:// 左 
            rightdownx=tank.x+60;
            rightdowny=tank.y+40;
            break;
       }
	if(bullet.x>tank.x&&bullet.x<rightdownx&&bullet.y>tank.y&&bullet.y<rightdowny){//击中
		tank.isalive=false;
		return true;
	}
}
function randommove(enemytank){//坦克随机移动
	switch(parseInt(3*Math.random())){
		case 0:
			enemytank.movetimer=window.setInterval("enemytank.moveUp()",500);
			break;
		case 1:
			enemytank.movetimer=window.setInterval("enemytank.moveRight()",500);
//			enemytank.moveRight();
			break;
		case 2:
			enemytank.movetimer=window.setInterval("enemytank.moveDown()",500);
//			enemytank.moveDown();
			break;
		case 3:
			enemytank.movetimer=window.setInterval("enemytank.moveLeft()",500);
//			enemytank.moveLeft();
			break;
	}
	window.setTimeout("window.clearInterval("+enemytank.movetimer+")",5000);
}
